package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.bo.SysTenantAddBo;
import com.ruoyi.system.bo.SysTenantEditBo;
import com.ruoyi.system.bo.SysTenantQueryBo;
import com.ruoyi.system.domain.SysTenant;
import com.ruoyi.system.mapper.SysTenantMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysTenantService;
import com.ruoyi.system.vo.SysTenantVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 租户Service业务层处理
 *
 * @author lingdeng
 * @date 2021-07-24
 */
@Service
public class SysTenantServiceImpl extends ServicePlusImpl<SysTenantMapper, SysTenant> implements ISysTenantService {

	@Autowired
	private SysUserMapper sysUserMapper;

	@Override
	public SysTenantVo queryById(Long tenantId) {
		return getVoById(tenantId, SysTenantVo.class);
	}

	@Override
	public TableDataInfo<SysTenantVo> queryPageList(SysTenantQueryBo bo) {
		PagePlus<SysTenant, SysTenantVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SysTenantVo.class);
		return PageUtils.buildDataInfo(result);
	}

	@Override
	public List<SysTenantVo> queryList(SysTenantQueryBo bo) {
		return listVo(buildQueryWrapper(bo), SysTenantVo.class);
	}

	private LambdaQueryWrapper<SysTenant> buildQueryWrapper(SysTenantQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<SysTenant> lqw = Wrappers.lambdaQuery();
		lqw.eq(bo.getTenantId() != null, SysTenant::getTenantId, bo.getTenantId());
		lqw.like(StrUtil.isNotBlank(bo.getTenantName()), SysTenant::getTenantName, bo.getTenantName());
		lqw.eq(StrUtil.isNotBlank(bo.getLeader()), SysTenant::getLeader, bo.getLeader());
		lqw.eq(StrUtil.isNotBlank(bo.getPhone()), SysTenant::getPhone, bo.getPhone());
		lqw.eq(StrUtil.isNotBlank(bo.getEmail()), SysTenant::getEmail, bo.getEmail());
		lqw.eq(StrUtil.isNotBlank(bo.getStatus()), SysTenant::getStatus, bo.getStatus());
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(SysTenantAddBo bo) {
		SysTenant add = BeanUtil.toBean(bo, SysTenant.class);
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean updateByEditBo(SysTenantEditBo bo) {
		SysTenant update = BeanUtil.toBean(bo, SysTenant.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(SysTenant entity) {
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if (isValid) {
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}

	/**
	 * 取消授权用户角色
	 *
	 * @param user 用户和角色关联信息
	 * @return 结果
	 */
	@Override
	public int deleteAuthUser(SysUser user) {
		SysUser entity = sysUserMapper.selectUserById(user.getUserId());
		entity.setUserType("02");
		return sysUserMapper.updateById(entity);
	}

	/**
	 * 批量取消授权用户租户
	 *
	 * @param tenantId 租户ID
	 * @param userIds  需要取消授权的用户数据ID
	 * @return 结果
	 */
	@Override
	public int deleteAuthUsers(Long tenantId, Long[] userIds) {
		SysUser entity = new SysUser();
		entity.setUserType("02");
		return sysUserMapper.update(entity, new LambdaQueryWrapper<SysUser>()
			.eq(SysUser::getUserType, "01")
			.in(SysUser::getUserId, Arrays.asList(userIds)));
	}

	/**
	 * 批量选择授权用户租户
	 *
	 * @param tenantId 租户ID
	 * @param userIds  需要删除的用户数据ID
	 * @return 结果
	 */
	@Override
	public int insertAuthUsers(Long tenantId, Long[] userIds) {
		// 新增用户与租户管理
		SysUser entity = new SysUser();
		entity.setUserType("01");
		entity.setTenantId(tenantId);
		return sysUserMapper.update(entity, new LambdaQueryWrapper<SysUser>()
			.eq(SysUser::getUserType, "02")
			.in(SysUser::getUserId, Arrays.asList(userIds)));
	}
}
